package com.qiezikaka.kaka.base;

import android.app.ActivityManager;
import android.content.Context;


import java.util.List;

public class AppStatusManager {

    /*垃圾回收机制会回收掉static关键字占用的内存*/
    private static AppStatusManager mInstance = null;
    private int appStatus = AppStatusConstant.APP_FORCE_KILLED;
    private int ALIVE_STACK_TOP = 0;
    private int ALIVE_STACK_BACKGROUND = 1;
    private int ALIVE_STACK_KILLED = 2;

    private AppStatusManager() {
    }
    public static AppStatusManager getInstance() {
        if(mInstance==null) {
            synchronized (AppStatusManager.class) {
                if(mInstance==null)
                    mInstance = new AppStatusManager();
            }
        }
        return mInstance;
    }

    public int isAppAlive(Context context, String packageName) {
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> listInfos = activityManager
                .getRunningTasks(20);
        /*判断程序是否在栈顶*/
        if (listInfos.get(0).topActivity.getPackageName().equals(packageName)) {
            return ALIVE_STACK_TOP;
        } else {
            /*判断程序是否在栈里*/
            for (ActivityManager.RunningTaskInfo info : listInfos) {
                if (info.topActivity.getPackageName().equals(packageName)) {
                    return ALIVE_STACK_BACKGROUND;
                }
            }
            /*栈里找不到,进程被杀死*/
            return ALIVE_STACK_KILLED;
        }
    }
    public void setAppStatus(int appStatus) {
        this.appStatus = appStatus;
    }
    public int getAppStatus() {
        return appStatus;
    }
}
